<template>
  <!--明细页面弹出框-->
  <el-dialog
    :title="formTitle"
    center
    :visible.sync="open"
    width="1024px"
    :close-on-click-modal="true"
    @close="closeFn"
  >
    <el-form ref="detailsForm" :model="formData" :rules="rules" label-width="120px" :disabled="formDisabled">
      <el-row>
        <el-col :span="8">
          <el-form-item label="交易流水号：" prop="issuedBrno">
            <el-input v-model="formData.id" style="width: 100%" :disabled="true"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="出票行行号：" prop="issuedBrno">
            <el-input v-model="formData.issuedBrno" @change="checkbankcode('issuedBrno')" style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-row>
            <el-button icon="el-icon-search" plain type="primary" @click="openBankBranchDialog('issuedBrno')">
              行名行号查询
            </el-button>
          </el-row>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="出票行行名：" prop="issuedBrname">
            <el-input v-model="formData.issuedBrname" :disabled="true" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="出票日期：" prop="issuedDate">
            <el-date-picker
              v-model="formData.issuedDate"
              type="date"
              value-format="yyyyMMdd"
              placeholder=""
              style="width: 100%"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="票据类型：" prop="draftType">
            <el-select style="width: 100%" v-model="formData.draftType" placeholder="">
              <el-option
                v-for="dict in dictMap.DRAFT_TYPE"
                :key="dict.value"
                :label="`${dict.value}-${dict.text}`"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="票据种类：" prop="draftCode">
            <el-select style="width: 100%" v-model="formData.draftCode" placeholder="">
              <el-option
                v-for="dict in dictMap.DRAFT_CODE"
                :key="dict.value"
                :label="`${dict.value}-${dict.text}`"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="申请人账号：" prop="issuedActno">
            <acct-no-input
              v-model="formData.issuedActno"
              placeholder="申请人账号"
              @keyup.enter.native="getActNoInfo"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-row>
            <el-button icon="el-icon-search" plain type="primary" @click="openPayerDialog('issuedActno')">
              账号查找
            </el-button>
          </el-row>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="申请人名称：" prop="issuedName">
            <el-input v-model="formData.issuedName" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="收款人名称：" prop="payeeName">
            <el-input v-model="formData.payeeName" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出票金额：" prop="issuedAmount">
            <ht-amount-input v-model="formData.issuedAmount" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="提示付款日期：" prop="promptDate">
            <el-date-picker
              v-model="formData.promptDate"
              type="date"
              value-format="yyyyMMdd"
              placeholder=""
              style="width: 100%"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex">
        <el-col :span="24">
          <el-form-item label="备注：" prop="remarks">
            <el-input type="textarea" v-model="formData.remarks" />
          </el-form-item>
        </el-col>
      </el-row>
      <div v-if="this.formData.draftType == '0'">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="代理付款行行号：" prop="agentBrno">
            <el-input v-model="formData.agentBrno" @change="checkbankcode('agentBrno')" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-row>
            <el-button icon="el-icon-search" plain type="primary" @click="openBankBranchDialog('agentBrno')">
              行名行号查询
            </el-button>
          </el-row>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="代理付款行行名：" prop="agentBrname">
            <el-input v-model="formData.agentBrname" :disabled="true" />
          </el-form-item>
        </el-col>
      </el-row>
      </div>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" v-show="!formDisabled" :loading="isLoading">提交</el-button>
      <el-button type="warning" @click="delFn" v-show="!formDisabled" :loading="isLoading">删除</el-button>
      <el-button @click="closeFn" >取消</el-button>
    </div>
    <bank-branch-dialog ref="bankBranchDialog" @confirm="confirmBankBranch"></bank-branch-dialog>
    <payer-dialog ref="payerDialog" @confirm="confirmPayer"></payer-dialog>
  </el-dialog>
</template>
<script>
import { approve, del } from "@/api/cnaps/bepsDraft/bank_note_issuance_update.js";

import BankBranchDialog from "@/views/components/BankBranchDialog";
import {submit} from "@/api/cnaps/bepsDraft/bank_note_issuance_entry";
import HtAmountInput from "@/views/components/HtAmountInput";
import { getCustByActno } from "@/api/cnaps/common/custInfo";
import PayerDialog from "@/views/components/PayerDialog";
import AcctNoInput from "@/views/components/AcctNoInput";
export default {
  name: "BankNoteIssuanceUpdateDetails",
  components: { AcctNoInput, PayerDialog, HtAmountInput, BankBranchDialog },
  props: {
    dictMap: {
      type: Object,
      default: () => {
        return {};
      },
    },
    formData: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      formDisabled: false,
      formTitle: "",
      // 是否显示弹出层
      open: false,
      isLoading: false,
      rules: {
        issuedBrno: [{ required: true, message: "不能为空", trigger: "change" }],
        draftType: [{ required: true, message: "不能为空", trigger: "change" }],
        draftCode: [{ required: true, message: "不能为空", trigger: "change" }],
        issuedActno: [{ required: true, message: "不能为空", trigger: ['blur', 'change']}],
        issuedName: [{ required: true, message: "不能为空", trigger: "blur" }],
        payeeName: [{ required: true, message: "不能为空", trigger: "blur" }],
        issuedAmount: [{ required: true, message: "不能为空", trigger: "blur" }],
        promptDate: [{ required: true, message: "不能为空", trigger: "change" }],
      },
    };
  },
  mounted() {},
  methods: {
    //行名行号返回
    confirmBankBranch(fromFlag, row) {
      if (fromFlag == "issuedBrno") {
        this.$set(this.formData, "issuedBrno", row.bankCode);
        this.$set(this.formData, "issuedBrname", row.orgFullname);
      } else if (fromFlag == "agentBrno") {
        this.$set(this.formData, "agentBrno", row.bankCode);
        this.$set(this.formData, "agentBrname", row.orgFullname);
      }
    },
    //行名行号查询
    openBankBranchDialog(fromFlag = "") {
      this.$refs.bankBranchDialog.show(fromFlag);
    },

    //审核
    submitForm() {
      this.$refs["detailsForm"].validate((valid) => {
        if (valid) {
          this.isLoading = true;
          approve({ ...this.formData })
            .then((res) => {
              this.isLoading = false;
              this.$emit("editComplete", true);
              this.msgSuccess("成功!");
              this.closeFn();
            })
            .catch((err) => {
              this.isLoading = false;
              //   this.$alert(`${res.result}`, "系统提示", {
              //     confirmButtonText: "确认",
              //     type: "error",
              //     callback: (action) => {},
            });
        }
      });
    },
    //拒绝
    delFn() {
      this.isLoading = true;
      del({ ...this.formData })
        .then((res) => {
          this.isLoading = false;
          this.$emit("editComplete", true);
          this.msgSuccess("成功!");
          this.closeFn();
        })
        .catch((err) => {
          this.isLoading = false;
          //   this.$alert(`${res.result}`, "系统提示", {
          //     confirmButtonText: "确认",
          //     type: "error",
          //     callback: (action) => {},
        });
    },
    getActNoInfo() {
      var actNo = this.formData.issuedActno;
      getCustByActno(actNo).then((response) => {
        if (response.code == "S" && response.result !== null) {
          this.$set(this.formData, "issuedActno", response.result.actNo);
          this.$set(this.formData, "issuedName", response.result.name);
        } else {
          this.msgSuccess("查无此账号");
          this.$set(this.form, "issuedName", null);
        }
      });
    },

    openPayerDialog(fromFlag = "") {
      this.$refs.payerDialog.show(fromFlag);
    },
    confirmPayer(fromFlag, row) {
      if (fromFlag == "issuedActno") {
        this.$set(this.formData, "issuedActno", row.actNo);
        this.$set(this.formData, "issuedName", row.name);
      }
    },

    // 表单重置
    reset() {
      this.resetForm("detailsForm");
    },
    closeFn() {
      this.open = false;
      this.reset();
    },
    show(type) {
      this.formTitle = "银行票据签发" + type;
      this.formDisabled = type === "明细" ? true : false;
      this.open = true;
    },
  },
};
</script>
<style>
.label-header-msg {
  font-weight: bold;
}

.form-card {
  margin-bottom: 10px;
}
</style>
